﻿Imports DevComponents.Editors.DateTimeAdv
Imports DevComponents.DotNetBar.Controls
Imports TNCBAL
Imports TNCDAL

Public Class ucSystemLog
    Private _logController As New LogController
    Private Sub ucLogging_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If GeneralClassBAL._User.ID_Role = enumROLE.ADMINISTRATOR Then
            PanelExCommand.Visible = True
        Else
            PanelExCommand.Visible = False
        End If
        InitSearchControls()
        ResetSearchControls(Me.Controls)
        BindingDataGridView()
        FormatDataGridView()
    End Sub


    Public Sub BindingDataGridView()
        'Lấy danh sách các File thuộc _enumFILE (hàng nhập hoặc hàng xuất)
        'Nếu chưa có lọc thì mặc định load tất cả
        Dim id_action As Integer = -1
        If cmbAction.SelectedIndex >= 0 Then
            id_action = cmbAction.SelectedValue
        End If
        Dim query = _logController.GetLogList(id_action, txtUser.Text, txtLogContent.Text, dtFromDate.Value, dtToDate.Value)

        Dim source As New BindingSource
        source.DataSource = query
        DataGridViewXLog.DataSource = query
        BindingNavigator1.BindingSource = source
    End Sub

    Public Sub FormatDataGridView()
        DataGridViewXLog.AllowUserToAddRows = False
        DataGridViewXLog.AllowUserToDeleteRows = False
        DataGridViewXLog.ReadOnly = True
        DataGridViewXLog.SelectionMode = DataGridViewSelectionMode.CellSelect
        DataGridViewXLog.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
    End Sub

#Region "Search Form"
    Private Sub ResetSearchControls(ByRef controlCollect As ControlCollection)
        For Each c As Control In controlCollect
            If c.Controls.Count > 0 Then
                ResetSearchControls(c.Controls)
            End If

            If TypeOf c Is TextBoxX Then
                CType(c, TextBoxX).Text = ""
            ElseIf TypeOf c Is TextBox Then
                CType(c, TextBox).Text = ""
            ElseIf (TypeOf c Is DateTimeInput) Then
                CType(c, DateTimeInput).Value = Now
            ElseIf (TypeOf c Is ComboBox) Then
                CType(c, ComboBox).SelectedIndex = -1
            ElseIf (TypeOf c Is ComboBoxEx) Then
                CType(c, ComboBoxEx).SelectedIndex = -1
            End If

            dtFromDate.Value = Now.Date.AddMonths(-3)
            dtToDate.Value = Now
        Next
    End Sub

    Private Sub InitSearchControls()
        Dim objActionRepository As New ActionRepository
        Dim query = objActionRepository.SelectAllRecord()
        cmbAction.ValueMember = "ID_Action"
        cmbAction.DisplayMember = "ActionName"
        cmbAction.DataSource = query
    End Sub

    Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
        BindingDataGridView()
    End Sub

    Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs) Handles btnCancel.Click
        ResetSearchControls(Me.Controls)
        Me.btnSearch.PerformClick()
    End Sub

#End Region

    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        If MessageBox.Show("Bạn thật sự muốn xóa hết dữ liệu?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
            _logController.DeleteAll()
            BindingDataGridView()
        End If
    End Sub
End Class
